//
//  DFTextField.m
//  DFTextField
//
//  Created by 如佳 陈 on 15/8/14.
//  Copyright (c) 2015年 crj. All rights reserved.
//

#import "DFTextField.h"

@implementation DFTextField

- (instancetype)init{
    if (self = [super init]) {
        [self initialize];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self initialize];
    }
    return self;
}

- (void)initialize{
    self.borderWidth = 1;
    self.sideHeight = 4;
    self.maxLength = INT16_MAX;
    [self addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)textFieldDidChange:(NSNotification*)not{
    if (self.text.length > self.maxLength) {
        self.text = [self.text substringToIndex:self.maxLength];
    }
}

- (void)willMoveToWindow:(UIWindow *)newWindow{
    self.transform = CGAffineTransformMakeScale(0, 1); //CGAffineTransformTranslate
    [UIView animateWithDuration:1 animations:^{
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
}

- (void)drawRect:(CGRect)rect {
    CGRect frame = self.frame;
    CGSize size = frame.size;
    CGFloat height = self.sideHeight;
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 0, size.height - height);
    CGContextAddLineToPoint(ctx, 0, size.height);
    CGContextAddLineToPoint(ctx, size.width, size.height);
    CGContextAddLineToPoint(ctx, size.width, size.height - height);
    CGContextSetLineWidth(ctx,self.borderWidth);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    [self.borderColor setStroke];
    CGContextDrawPath(ctx, kCGPathStroke);
    self.tintColor = self.borderColor;
}

@end
